<template>
    <el-dialog
        :title="title"
        append-to-body
        destroy-on-close
        :model-value="showDialog"
        @close="closeDialog()"
    >
        <!-- 表单 -->
        <el-form
            :model="form"
            ref="formRef"
            :rules="rules"
            label-width="80px"
            :inline="false"
        >
            <el-form-item label="客户端ID" prop="clientId">
                <el-select v-model="clientId" placeholder="客户端ID" clearable>
                    <el-option
                        v-for="item in clientList"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                        :namespace="item"
                    />
                </el-select>
            </el-form-item>
            <el-form-item label="路由IP" prop="routeIp">
                <el-input v-model="routeIp" placeholder="" clearable></el-input>
            </el-form-item>
            <el-form-item label="描述" prop="describe">
                <el-input
                    v-model="describe"
                    placeholder=""
                    clearable
                ></el-input>
            </el-form-item>
        </el-form>

        <template v-slot:footer>
            <span>
                <el-button @click="closeDialog()">取消</el-button>
                <el-button type="primary" @click="onSubmit()">确定</el-button>
            </span>
        </template>
    </el-dialog>
</template>

<script setup>
import {
    reactive,
    toRefs,
    ref,
    defineProps,
    defineEmits,
    onMounted,
} from "vue";

const rules = {
    clientId: [
        {
            required: true,
            message: "请选择客户端",
            trigger: "blur",
        },
    ],
    routeIp: [
        {
            required: true,
            message: "请输入路由IP",
            trigger: "blur",
        },
    ],
};
const props = defineProps({
    showDialog: {
        type: Boolean,
        default: true,
    },
    title: {
        type: String,
        default: "添加",
    },
    rowData: {
        type: Object,
        default: null,
    },
});
const emit = defineEmits(["closeDialog"]);
const { title, rowData } = toRefs(props);
const clientList = ref(null);

const closeDialog = () => {
    emit("closeDialog", false);
};
const formRef = ref(null);

const form = reactive({
    clientId: "",
    routeIp: "",
    describe: "",
});
const { clientId, routeIp, describe } = toRefs(form);

/**
 * @description: 初始化
 * @param {*}
 * @return {*}
 */
rowData.value &&
    ((clientId.value = rowData.value.clientId),
    (routeIp.value = rowData.value.routeIp),
    (describe.value = rowData.value.describe));
/**
 * @description:提交
 * @param {*}
 * @return {*}
 */
const onSubmit = () => {
    formRef.value.validate(async (valid) => {
        if (valid) {
            let res;
            if (title.value === "添加") {
                res =
                    await VE_API.cloudNetworkRoute.lazyNettyClientRouteSave(
                        form,
                    );
            } else {
                res = await VE_API.cloudNetworkRoute.lazyNettyClientRouteUpdate(
                    {
                        id: rowData.value.id,
                        ...form,
                    },
                );
            }
            const { code } = res;
            if (code === 0) {
                closeDialog();
            }
        } else {
            console.log("error submit!!");
            return false;
        }
    });
};

const findClientList = async () => {
    VE_API.cloudNetwork.cloudClientFindListGroupByClient({}).then((res) => {
        if (res.code === 0) {
            if (res.data) {
                res.data.map((item) => {
                    item.label = item.clientId;
                    item.value = item.clientId;
                });
                clientList.value = res.data ? res.data : [];
            }
        }
    });
};
onMounted(async () => {
    await findClientList();
});
</script>

<style lang="scss" scoped></style>
